let User = require('../model/user')
let Links = require('../model/links')
let result = require('../utils/result')
let format = require('../utils/format.js')
function LinksController(){

};

/**
 * 查询所有用户
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
LinksController.prototype.getLinks = async (req,res)=>{
    let uid = req.body.uid;
    if(!uid){
        res.json(result(-1,'请求参数不合法'))
        return;
    }
    let links = await Links.findAll({where: {uid:uid,isDel: 0}});
    res.status(200).json(result(0,'success',links))
}

LinksController.prototype.createLinks = async (req,res)=>{
    let uid = req.body.uid;
    let links = req.body.links;
    let remark = req.body.remark;
    if(!uid){
        res.json(result(-1,'请求参数不合法'))
        return;
    };
    if(!links){
        res.json(result(-1,'请求参数不合法'))
        return;
    }
    Links.create({links: links,uid:uid}).then(rsp=>{
        res.json(result(0,'success'))
    }).catch(()=>{
        res.json(result(-1,'fail'))
    })
}

LinksController.prototype.delLinks = async (req,res)=>{
    let uid = req.body.uid;
    let id = req.body.id;
    if(!uid){
        res.json(result(-1,'请求参数不合法'))
        return;
    };
    if(!id){
        res.json(result(-1,'请求参数不合法'))
        return;
    }
    Links.update({isDel:1},{where:{uid:uid,id:id}}).then(rsp=>{
        res.json(result(0,'success'))
    }).catch(()=>{
        res.json(result(-1,'fail'))
    })
}




let linksController = new LinksController();
module.exports = linksController
